//
//  LHMapViewController.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/13.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHMapViewController.h"
#import <BaiduMapAPI/BMKMapView.h>
#import "MyNavgationView.h"
#import <BaiduMapAPI/BMapKit.h>
#import "LHSearchMapView.h"

@interface LHMapViewController ()<BMKMapViewDelegate, BMKMapViewDelegate, BMKPoiSearchDelegate>
@property (nonatomic, retain)BMKMapView* mapView;
@property (nonatomic, retain)MyNavgationView *mapNavigation;
@property (nonatomic, retain)BMKLocationService *locService;
@property (nonatomic, retain)BMKPoiSearch *poisearch;
@property (nonatomic, retain)LHSearchMapView *searchView;
@end

@implementation LHMapViewController

-(void)dealloc
{
    [_searchView release];
    [_locService release];
    [_CityNameStr release];
    [_keyWordName release];
    [_userLocations release];
    [_mapNavigation release];
    [_poisearch release];
    [_mapView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self creatNavigation];
    
    //建地图
    self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [self.view addSubview:_mapView];
    [_mapView release];
    
    //初始化搜索
    self.poisearch = [[[BMKPoiSearch alloc]init]autorelease];
    _poisearch.delegate = self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    BMKCitySearchOption *citySearchOption = [[[BMKCitySearchOption alloc]init]autorelease];
    citySearchOption.pageIndex = 0;
    citySearchOption.pageCapacity = 50;
    NSLog(@"%@",self.CityNameStr);
    NSLog(@"%@",self.keyWordName);
    citySearchOption.city= self.CityNameStr;
    citySearchOption.keyword =self.keyWordName;
    BOOL flag = [_poisearch poiSearchInCity:citySearchOption];
    if(flag)
    {
        NSLog(@"城市内检索发送成功");
    }
    else
    {
        NSLog(@"城市内检索发送失败");
    }

}


- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    // 生成重用标示identifier
    NSString *AnnotationViewID = @"xidanMark";
    
    // 检查是否有重用的缓存
    BMKAnnotationView* annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    
    // 缓存没有命中，自己构造一个，一般首次添加annotation代码会运行到此处
    if (annotationView == nil) {
        annotationView = [[[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]autorelease];
        ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
        // 设置重天上掉下的效果(annotation)
        ((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
    }
    
    // 设置位置
    annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
    annotationView.annotation = annotation;
    // 单击弹出泡泡，弹出泡泡前提annotation必须实现title属性
    annotationView.canShowCallout = YES;
    // 设置是否可以拖拽
    annotationView.draggable = NO;
    
    return annotationView;
}

- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    // 清楚屏幕中所有的annotation
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    
    if (error == BMK_SEARCH_NO_ERROR) {
        for (int i = 0; i < result.poiInfoList.count; i++) {
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
            BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
            item.coordinate = poi.pt;
            item.title = poi.name;
            [_mapView addAnnotation:item];
            [item release];
            if(i == 0)
            {
                //将第一个点的坐标移到屏幕中央
                _mapView.centerCoordinate = poi.pt;
            }
        }
    } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        NSLog(@"起始点有歧义");
    } else {
        // 各种情况的判断。。。
    }
}

/**
 *  创建导航条
 */
- (void)creatNavigation
{
    self.mapNavigation = [[MyNavgationView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [self.mapNavigation creatTitle:@"地图"];
    _mapNavigation.leftAction = @selector(backToSearch);
    _mapNavigation.leftTarget = self;
    [_mapNavigation creatLeftButtonWithImage:@"btn_nav_back"];
    [self.view addSubview:_mapNavigation];
    [_mapNavigation release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

- (void)backToSearch
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
